Page({
  data: {
    code:''
  },
  login(){
    const that = this
    that.getUserInfo()
  },
  getUserInfo(){
    wx.getUserProfile({
      desc: 'desc',
      success:(res)=>{
        console.log(res)
        wx.login({
          success: (res) => {
            if(res.code){
              console.log(res.code)
              wx.request({
                url: 'https://zhixiao.site:8080/client/getOpenId',
                data:{
                  code:res.code
                },
                //后端经过处理，传递给前端openid和session_key
                success:(res1) => {
                  console.log(11)
                  console.log(res1)
                  wx.setStorageSync('openid', res1.data.data.openid)
                  wx.setStorageSync('session_key', res1.data.data.session_key)
                  //根据openid来进行不同身份的登录
                  wx.request({
                    url: 'https://zhixiao.site:8080/client/login',
                    data:{
                      openid:wx.getStorageSync('openid')
                    },
                    //后端返回用户对应身份，养老院名称
                    success:(res2)=>{
                      console.log(1212)
                      console.log(res2)
                      var JSESSIONID = res2.cookies[0]
                      wx.setStorageSync('JSESSIONID', JSESSIONID)
                      wx.setStorageSync('userid', res2.data.data.id)
                      wx.setStorageSync('identity', res2.data.data.identity)
                      wx.setStorageSync('beadhouse', res2.data.data.careHomeName)
                      var bindMsg = wx.getStorageSync('bindMsg')===''?[]:wx.getStorageSync('bindMsg')
                      wx.setStorageSync('bindMsg', bindMsg)
                    },
                    complete:(res2)=>{
                      if(wx.getStorageSync('nickName')===''||wx.getStorageSync('avatarUrl')==='')
                      {
                        wx.redirectTo({
                          url: '/subpkg/getnickName/getnickName',
                        })
                      }
                     else{
                      if(res2.data.data.identity==='老人'){
                        console.log("老人")
                        wx.reLaunch({
                          url: '/elder/service/service',
                        })
                      }else if(res2.data.data.identity==='护工'){
                        console.log("护工")
                        wx.reLaunch({
                          url: '/pages/elderList/elderList',
                        })
                      }else{
                        console.log("家属")
                        wx.reLaunch({
                          url: '/pages/home/home',
                        })
                      }
                     }
                    }
                  })
                }
              })
            }
          },
        })
      },
      fail:()=>{
        console.log("登录失败")
      }
    })
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    if(wx.getStorageSync('openid')===''||wx.getStorageSync('session_key'==='')||wx.getStorageSync('JSESSIONID')===''){
      wx.hideLoading()
      return
    }
    this.queryIdentity()
  },
  queryIdentity(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/login',
      data:{
        openid:wx.getStorageSync('openid')
      },
      //后端返回用户对应身份，养老院名称
      success:(res2)=>{
        console.log(1212)
        console.log(res2)
        var JSESSIONID = res2.cookies[0]
        wx.setStorageSync('JSESSIONID', JSESSIONID)
        wx.setStorageSync('userid', res2.data.data.id)
        wx.setStorageSync('identity', res2.data.data.identity)
        wx.setStorageSync('beadhouse', res2.data.data.careHomeName)
        var bindMsg = wx.getStorageSync('bindMsg')===''?[]:wx.getStorageSync('bindMsg')
        wx.setStorageSync('bindMsg', bindMsg)
      },
      complete:(res2)=>{
        if(wx.getStorageSync('nickName')===''||wx.getStorageSync('avatarUrl')==='')
        {
          wx.redirectTo({
            url: '/subpkg/getnickName/getnickName',
          })
        }
       else{
        if(res2.data.data.identity==='老人'){
          console.log("老人")
          wx.reLaunch({
            url: '/elder/service/service',
          })
        }else if(res2.data.data.identity==='护工'){
          console.log("护工")
          wx.reLaunch({
            url: '/pages/elderList/elderList',
          })
        }else{
          console.log("家属")
          wx.reLaunch({
            url: '/pages/home/home',
          })
        }
        wx.hideLoading()
       }
      }
    })
  },
  onReady() {
  },
  onShow() {
  },
  onHide() {
  },
  onUnload() {
  },
  onPullDownRefresh() {
  },
  onReachBottom() {
  },
  onShareAppMessage() {
  }
})